package com.my.juc.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/6 15:23
 */

/**
 * 写锁可降级为读锁
 * 读锁不可升级为写锁
 */
public class Demo2 {

    public static void main(String[] args) {
        // 可重入读写锁对象
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();
        
        // 锁降级
        // 写锁
        writeLock.lock();
        // 读锁
        readLock.lock();
        System.out.println("------read");
        System.out.println("readWrite");
        
        
        // 释放写锁
        writeLock.unlock();
        
        // 释放读锁
        readLock.unlock();
        
    }
    
}
